<?php

namespace app\business\controller;

use think\Db;
use think\Facade\Config as Configs;
use EasyWeChat\Factory;


class Main extends Base
{
    public function welcome()
    {
        $bussiness = $this->request->bussiness;
        return view(null, [
            'bussiness' => $bussiness
        ]);
    }

    public function index()
    {
        $bussiness = $this->request->bussiness;
        $menus = config('bmenu.menu');
        $menu = [];
        foreach ($menus as $k => $v) {
            if ($v['show'] == true) {
                foreach ($v['sub'] as $x => $y) {
                    $menu[$k]['name'] = $v['name'];
                    $menu[$k]['ico'] = $v['ico'];
                    $menu[$k]['sub'][$x] = $y;
                }
            }
        }
        $name = 'business' . $this->request->bussiness['id'] . '.png';
        $qcode = url('/wechat/' . $name, '', '', true);
        try {
            $path =  $_SERVER['DOCUMENT_ROOT'] . "/wechat/";
            if (!file_exists($path . $name)) {
                $config = config('wechat.');
                $app = Factory::miniProgram($config);
                $rm = $app->app_code->getUnlimit('shopid=' . $this->request->bussiness['id'], [
                    'page'  => '',
                    'width' => 600,
                ]);
                if ($rm instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
                    $filename = $rm->save($path, $name);
                }
            }
        } catch (\Throwable $th) {
        }
        return view(null, [
            'menu' => $menu,
            'bussiness' => $bussiness,
            'qcode' => $qcode
        ]);
    }


    public function miss()
    {
        return view('404');
    }
}
